//
//  LWAddressViewController.m
//  LWShopItem
//
//  Created by mac on 17/5/22.
//  Copyright © 2017年 LWShop.com. All rights reserved.
//

#import "LWAddressViewController.h"
#import "LWAccountModel.h"
#import "LWProvinceTabVc.h"
#import "LWCityTabVc.h"
#import "LWCountryTabVc.h"

@interface LWAddressViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *NameTextFild;//收货人姓名

@property (copy,nonatomic)NSString *IDlabel;

@property (copy,nonatomic)NSString *countryID;
@property(nonatomic,copy)NSString *villageID;

@property (weak, nonatomic) IBOutlet UILabel *ProvinceLabel;

@property (weak, nonatomic) IBOutlet UILabel *CityLabel;

@property (weak, nonatomic) IBOutlet UILabel *countryLabel;

@property (weak, nonatomic) IBOutlet UITextField *LocationTextFild;//详细地址

@property (weak, nonatomic) IBOutlet UITextField *PhoneNumberTextFild;//联系电话

@property (weak, nonatomic) IBOutlet UITextField *PostcodeTextFild;//邮政编码

@property(nonatomic, weak) UIBarButtonItem *rightItem;



@end

@implementation LWAddressViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textField) name:UITextFieldTextDidChangeNotification object:nil];
    //
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textField) name:UITextFieldTextDidBeginEditingNotification object:nil];
    //
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textField) name:UITextFieldTextDidEndEditingNotification object:nil];
    
    self.NameTextFild.text = _model.consignee;
    
    self.PhoneNumberTextFild.text = _model.mobile;
    
    self.LocationTextFild.text = _model.address;
    
    
    
    [self setupUI];
    
    
    //省份
    UITapGestureRecognizer *tagProvince = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(getProvince)];
    
    self.ProvinceLabel.userInteractionEnabled = YES;
    
    [self.ProvinceLabel addGestureRecognizer:tagProvince];
    
    //城市
    UITapGestureRecognizer *tagCity = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(getCity)];
    
    self.CityLabel.userInteractionEnabled = YES;
    
    [self.CityLabel addGestureRecognizer:tagCity];
    
    //区县
    UITapGestureRecognizer *tagCountry = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(getCountry)];
    
    self.countryLabel.userInteractionEnabled = YES;
    
    [self.countryLabel addGestureRecognizer:tagCountry];
    
    
}
#pragma MARK - 省市区县
- (void)getProvince
{
    NSLog(@"点击了获取省份");
    
    LWProvinceTabVc *vc = [LWProvinceTabVc new];
    
    //如果拿到
    if (vc != nil) {
        
        //准备代码快
        void(^comptlion)() = ^(LWProvinceModel *model){
            
            self.ProvinceLabel.text = model.name;
            
            self.IDlabel = model.id;
            
            
            
        };
        
        //属性赋值
        vc.completion = comptlion;
        
        
        
    };
    
    
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)getCity
{
    NSLog(@"点击了获取城市");
    LWCityTabVc *vc = [LWCityTabVc new];
    
    vc.ID = self.IDlabel;
    
    if (vc != nil) {
        
        //准备代码快
        void(^comptlion)() = ^(LWProvinceModel *model){
            
            self.CityLabel.text = model.name;
            
            
            self.countryID = model.id;
            
        };
        
        //属性赋值
        vc.completion = comptlion;
        
        
        
    };
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)getCountry
{
    NSLog(@"点击了获取区县");
    LWCountryTabVc *vc = [LWCountryTabVc new];
    
    vc.CountryID = self.countryID;
    
    if (vc != nil) {
        
        //准备代码快
        void(^comptlion)() = ^(LWProvinceModel *model){
            
            self.countryLabel.text = model.name;
            self.villageID = model.id;
        };
        
        //属性赋值
        vc.completion = comptlion;
        
        
        
    };
    
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)textFieldDidEndEditing:(UITextField *)textField
{
    [self.NameTextFild resignFirstResponder];
    
}
#pragma MARK - ui
- (void)setupUI
{
    UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(popClick)];
    self.navigationItem.leftBarButtonItem = backBtnItem;
    
    
    //判断模型是否有值
    if (_model != nil) {
        
        self.title = @"联系人详情";
        
        UIBarButtonItem *savItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(savItemClick)];
        
        self.rightItem = savItem;
        //savItem.enabled = NO;
        self.navigationItem.rightBarButtonItem = savItem;
        
        return;
        
    }
    
    self.title = @"添加联系人";
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(addItemClick)];
    
    self.rightItem = rightItem;
    
    //rightItem.enabled = NO;
    
    self.navigationItem.rightBarButtonItem = rightItem;
    
    
    
}
#pragma MARK - addItemClick[添加]
- (void)savItemClick
{
    
    NSLog(@"点击确定按钮");
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    dic[@"consignee"] = self.NameTextFild.text;//联系人
    
    dic[@"province"] = self.IDlabel;//省份
    
    //这个方法不可行
    // dic[@"province"] = [[NSUserDefaults standardUserDefaults]objectForKey:@"id"];
    
    dic[@"city"] = self.countryID;//城市
    
    dic[@"district"] = self.villageID;//区县
    
    dic[@"address"] = self.LocationTextFild.text;//详细地址
    
    dic[@"mobile"] = self.PhoneNumberTextFild.text;//电话
    
    dic[@"PostcodeTextFild"] = self.PostcodeTextFild.text;//邮政编码
    
    dic[@"token"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
    
    dic[@"user_id"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"];
    
    [HYBNetworking postWithUrl:@"index.php?m=Api&c=User&a=addAddress" refreshCache:YES params:[LWUtilityClass isDict:dic] success:^(id response) {
        
        [[NSUserDefaults standardUserDefaults] setValue:response[@"result"] forKey:@"result"];
        NSLog(@"============");
        NSLog(@"%@",response[@"result"]);
        NSLog(@"============");
        
        
    } fail:^(NSError *error) {
        
    }];
    
    //先修改数据
    _model.consignee = _NameTextFild.text;
    _model.mobile = _PhoneNumberTextFild.text;
    _model.address = [NSString stringWithFormat:@"%@%@%@%@",_ProvinceLabel.text,_CityLabel.text,_countryLabel.text,_LocationTextFild.text];
    
    //通过代理 让列表刷新
    if ([_delegate respondsToSelector:@selector(detilVcDidFinishEndingContact:)]) {
        
        [_delegate detilVcDidFinishEndingContact:self];
    }
    
    //pop
    [self.navigationController popViewControllerAnimated:YES];
    
    
}
#pragma Mark - 右侧按钮
- (void)addItemClick {
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    dic[@"consignee"] = self.NameTextFild.text;//联系人
    
    dic[@"province"] = self.IDlabel;//省份
    
    dic[@"city"] = self.countryID;//城市
    
    dic[@"district"] = self.villageID;//区县
    
    dic[@"address"] = self.LocationTextFild.text;//详细地址
    
    dic[@"mobile"] = self.PhoneNumberTextFild.text;//电话
    
    dic[@"PostcodeTextFild"] = self.PostcodeTextFild.text;//邮政编码
    
    dic[@"token"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
    
    dic[@"user_id"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"];
    
    
    [HYBNetworking postWithUrl:@"index.php?m=Api&c=User&a=addAddress" refreshCache:YES params:[LWUtilityClass isDict:dic] success:^(id response) {
        
        
        [[NSUserDefaults standardUserDefaults] setValue:response[@"result"] forKey:@"result"];
        
        NSLog(@"%@",response[@"result"]);
        
        
        
    } fail:^(NSError *error) {
        
    }];
    
    
    //创建新的模型
    LWAccountModel *newModel = [LWAccountModel new];
    
    //给模型赋值
    newModel.province = self.provinceName;
    newModel.city = _CityLabel.text;
    newModel.district = _countryLabel.text;
    
    newModel.consignee = _NameTextFild.text;
    newModel.mobile = _PhoneNumberTextFild.text;
    newModel.address =  [NSString stringWithFormat:@"%@%@%@%@",_ProvinceLabel.text,_CityLabel.text,_countryLabel.text,_LocationTextFild.text];
    
    
    //通过代理让列表控制器刷新数据
    if ([_delegate respondsToSelector:@selector(detilVc:addModel:)]) {
        
        [_delegate detilVc:self addModel:newModel];
        
    }
    
    
    //pop
    [self.navigationController popViewControllerAnimated:YES];
    
    
    
}

#pragma MARK - POP
- (void)popClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

//- (void)textField {
//    if (self.NameTextFild.hasText &&
//        self.ProvinceLabel.hasText &&
//        self.CityTextFild.hasText &&
//        self.CountyTextFild.hasText &&
//        self.LocationTextFild.hasText &&
//        self.PhoneNumberTextFild.hasText &&
//        self.PostcodeTextFild.hasText) {
//        self.rightItem.enabled = YES;
//    }else {
//        self.rightItem.enabled = NO;
//    }
//}
@end
